package com.author.surface;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;

import javax.swing.JToggleButton;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;
import javax.swing.text.StyledEditorKit;

public final class BoldAction extends JToggleButton {
	/** Width and height of the BOLD action button */
	private static final int	BOLD_BUTTON_WIDTH	= 30;
	private static final int	BOLD_BUTTON_HEIGHT	= 30;

	/** Font used to display the "B" letter inside the BOLD action button */
	private static final Font	boldButtonFont		= new Font("Courier New",
															Font.BOLD, 24);

	/**
	 * X and Y coordinates which will be used to position the "B" letter inside
	 * the BOLD action button
	 */
	private static final int	TX_					= 8;
	private static final int	TY_					= 23;

	/** characters that are used for display inside the BOLD action button */
	public static final String	BOLD				= "B";

	/** Bold action for the Styled Editor kit */
	private static class boldAction extends StyledEditorKit.BoldAction {
		public boldAction() {
			super();
			putValue(NAME, ""); // name is not used
			putValue(ACCELERATOR_KEY,
					KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK));
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			super.actionPerformed(event);
		}
	};

	/**
	 * Bold action when invoked by the user will set the characters in the pane
	 * bold. This will usually be invoked through the CTRL+B combination of
	 * keystrokes
	 */
	private static boldAction	bold_action	= new boldAction();

	public BoldAction() {
		// Add bold action to the Toggle Button
		super(bold_action);

		// Set size of the BOLD button
		setSize(new Dimension(BOLD_BUTTON_WIDTH, BOLD_BUTTON_HEIGHT + 1));
		setPreferredSize(getSize());
		setEnabled(true);

		// Set border of the BOLD button
		setBorder(new BevelBorder(BevelBorder.LOWERED));
		setBorderPainted(false);

		// Set visible properties
		setToolTipText("Bold");
		setFocusable(false);
		setVisible(true);

		// Make the button invoke the action when a specific sequence of
		// keystrokes is fired by the user
		getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
				KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK),
				"ctrl b");
		getActionMap().put("ctrl b", bold_action);
	}

	/**
	 * This method will be executed when the UI updater requests that the button
	 * responsible for displaying Bold font is either selected or de-selected
	 */

	public void setBold(boolean value) {
		if (value == true) {
			if (!isSelected()) {
				setSelected(true);
			}
		}

		if (value == false) {
			if (isSelected()) {
				setSelected(false);
			}
		}
	}

	public void paint(Graphics g) {
		super.paint(g);

		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		FontRenderContext frc = g2d.getFontRenderContext();
		GlyphVector glyphs = boldButtonFont.createGlyphVector(frc, BOLD);

		g2d.drawGlyphVector(glyphs, TX_, TY_);
	}
}
